package com.mapper;
import com.mapper.dao.TUserDao;
import com.mapper.model.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

//import static jdk.nashorn.internal.codegen.LocalVariableTypesCalculator.assertNotNull;

public class App {
    public static void main(String[] args) throws IOException {
        String resource = "mybatisConfig.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //使用session会话对象
        /*Mybatis 中可以设置自动提交功能
           在利用工厂模式获得SqlSession实现类对象时，
           采用openSession(true)，即可实现自动提交，
           无需调用commit()方法。
         */
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        //List<User> list = sqlSession.selectList("user.selectAll");
        TUserDao userDao = sqlSession.getMapper(TUserDao.class);
//        RoleDao roleDao = sqlSession.getMapper(RoleDao.class);

        List<User> userRoles = userDao.getUsersByRole(1);// 传入用户编号

        User user = userRoles.get(0);
        System.out.println("编号: "+user.getUserId());
        System.out.println("姓名: "+user.getUsername());
        System.out.println("性别: "+user.getUserGender());
        System.out.println("生日: "+user.getBirthday());
        System.out.println( user.getRoles().get(0).toString());


        System.out.println(userDao.getRoleUsersByRole(1).size());// 传入权限ID

    }
}


